﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConfeccionesJR.Entites;
using System.Data;
using System.Data.Objects;

namespace ConfeccionesJR.Repository
{
    public class DetalleCredRepository: MasterDataAccess, IDetalleCredRepository
    {
        public DETALLE_CREDITO GetDetalleCreditoByCreditoIdAndProductoId(int IdDetalle,  int idCredito, int idProd)
        {
            return Context.DETALLE_CREDITO.Include("PRODUCTO")
                .Where(
                 c => c.id.Equals(IdDetalle) && c.PRODUCTO.Id.Equals(idProd) && c.CreditoId.Equals(idCredito)
                ).FirstOrDefault();
                //.Where(c => c.PRODUCTO.Id.Equals(idProd) && c.CreditoId.Equals(idCredito)).SingleOrDefault();
        
        }

        public List<DETALLE_CREDITO> GetFromDetalleCreditoByProductoDescAndCreditoId(string descripcion, int idCredito)
        {
            if (descripcion != "")
            {
                var query = from c in Context.DETALLE_CREDITO.Include("PRODUCTO")
                            where c.PRODUCTO.Descripcion.ToUpper().Contains(descripcion.ToUpper())
                            && c.CreditoId.Equals(idCredito)
                            select c;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);

                return query.ToList();
            }
            else
            {
                var query = from c in Context.DETALLE_CREDITO.Include("PRODUCTO")
                            where c.CreditoId.Equals(idCredito)
                            orderby c.FechaDetalleCredito descending
                            select c;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);

                return query.ToList();
            }
        }

        public bool GetAllDetallesCredByCreditoId(int idCredito)
        {
            var query = from c in Context.DETALLE_CREDITO.Include("CREDITO")
                        where c.CreditoId.Equals(idCredito)
                        select c;
            
            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.Any();
        }

        public void UpdateDetalleCreditoByCreditoIdAndDetalleId(int CreditoId, int idDetallCredito)
        {
            var existe = Context.DETALLE_CREDITO.Where(c => c.CreditoId.Equals(CreditoId) 
                && c.Pagado == false && c.id.Equals(idDetallCredito)).FirstOrDefault();

            existe.Pagado = true;
            Context.Entry(existe).State = EntityState.Modified;
            Context.SaveChanges();
        }

        public void PasarDetalleCreditoToVentaByIdAndCreditoId(int idDetallCredito, int CreditoId)
        {
            var existe = Context.DETALLE_CREDITO.Where(c => c.CreditoId.Equals(CreditoId)
                && c.Pagado == true && c.PasadoVenta == false && c.id.Equals(idDetallCredito)).FirstOrDefault();

            existe.PasadoVenta = true;
            Context.Entry(existe).State = EntityState.Modified;
            Context.SaveChanges();
        }
        
        public void RemoveDetalleCreditoByAndCreditoId(int Id, int IdCredito)
        {
            var existe = Context.DETALLE_CREDITO.Where(c => c.CreditoId.Equals(IdCredito) && c.id.Equals(Id)).FirstOrDefault();

            Context.DETALLE_CREDITO.Remove(existe);
                
            Context.SaveChanges();
        }



        
    }
}
